package com.android.pandoid;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.PointFEvaluator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.PointF;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.view.View;
import android.widget.Button;

import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = findViewById(R.id.buttonConnect);
        final Button button2 = findViewById(R.id.button2);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                animationM(button);
//                managerStart();
//                calendarIntent();
//                startSerViceActivity();
                startPhoneCallViewActivity();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {


            }
        });
    }

    private void startPhoneCallActivity() {
        Intent intent = new Intent(this, PhoneCallActivity.class);
        startActivity(intent);
    }
    private void startPhoneCallViewActivity() {
        Intent intent = new Intent(this, PhoneCallViewActivity.class);
        startActivity(intent);
    }

    private void startSerViceActivity() {
        Intent intent = new Intent(this, Binding.class);
        startActivity(intent);
    }

    private void animationM(final Button button) {
        //        ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
        ValueAnimator animation = ValueAnimator.ofObject(new PointFEvaluator(), new PointF(0, 200), new PointF(200, 200));
        animation.setDuration(1000);
        animation.start();
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animatedValuex = ((PointF) animation.getAnimatedValue()).x;
                float animatedValuey = ((PointF) animation.getAnimatedValue()).y;
                button.setTranslationX(animatedValuex);
                button.setTranslationY(animatedValuey);
            }
        });
    }

    private void managerStart() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        Uri uri = Uri.parse("http://www.baidu.com/index");
        intent.setData(uri);
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        boolean isIntentSafe = activities.size() > 0;
        if (isIntentSafe) {
            startActivity(intent);
        }
    }

    private void calendarIntent() {
        Intent calendarIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
        Calendar beginTime = Calendar.getInstance();
        beginTime.set(2020, 8, 19, 7, 30);
        Calendar endTime = Calendar.getInstance();
        endTime.set(2020, 8, 19, 10, 30);
        calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
        calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
        calendarIntent.putExtra(CalendarContract.Events.TITLE, "Ninja class");
        calendarIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Secret dojo");
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(calendarIntent,
                PackageManager.MATCH_DEFAULT_ONLY);
        boolean isIntentSafe = activities.size() > 0;
        if (isIntentSafe) {
            startActivity(calendarIntent);
        }
    }
}